/*
 * @Author: hrg
 * @Date: 2024-01-23 09:38:25
 * @LastEditTime: 2024-01-23 16:51:51
 * @FilePath: \study_go\study06_gin\gin_demo05\main.go
 * @Description:
 */
package main

import (
	"net/http"

	"github.com/gin-gonic/gin"
)

// 定义结构体
type User struct {
	Name string `json:"Name" binding:"required"`
	Age  int    `json:"Age" binding:"required"`
}

func AddUser(c *gin.Context) {
	// 创建 User 结构体实例

	var newUser User

	// 如果有错误，就进入下面代码
	if err := c.BindJSON(&newUser); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{
			"error": err.Error(),
		})
	} else {
		// 没有错误就返回
		c.JSON(http.StatusOK, newUser)

	}

}

// 将文件从本地传递到网页中
func DownLoad(c *gin.Context) {
	// filePath := "./Static/ca.txt"
	filePath := "./Static/01.png"

	c.File(filePath)
}

func Video(c *gin.Context) {
	filepath := "./Static/001.mp4"
	c.File(filepath)
}

func main() {
	r := gin.New()

	r.POST("/adduser", AddUser)

	r.GET("/111", DownLoad)
	r.GET("/222", Video)

	r.Run(":8181")

}
